package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.ktjy.pojo.vo.SysUserVo;
import com.ktjy.service.SysRoleService;
import com.ktjy.service.SysUserService;
import com.ktjy.unti.MD5Unti;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;

import java.util.Map;

/**
 * 用户管理
 */
@Controller
public class UserController {
    @Autowired
    SysUserService userService;
    @Autowired
    SysRoleService roleService;

    @RequestMapping("/user/list")
    public Object UserManagement(
            @RequestParam(defaultValue = "1") Integer pageIndex,
            @RequestParam(defaultValue = "2") Integer pageSize,
            @RequestParam(defaultValue = "0") Long roleId, Model model,
            SysUserVo sysUserVo) {
        IPage<SysUserVo> userVoIPage = new Page<>(pageIndex, pageSize);
        sysUserVo.setUsrRoleId(roleId);
        model.addAttribute("userPager", userService.getUserList(userVoIPage, sysUserVo));
        model.addAttribute("roles", roleService.list());
        model.addAttribute("usrName", sysUserVo.getUsrName());
        model.addAttribute("roleId", sysUserVo.getUsrRoleId());
        return "user/list";
    }

    @RequestMapping("/user/add")
    public String getAdd(Model model) {
        model.addAttribute("roles", roleService.list());
        return "user/add";
    }

    @RequestMapping("/user/save")
    public String getSave(SysUserVo sysUserVo) {
        if (sysUserVo.getUsrId()!=0){
            SysUserVo sysUserVo1 = userService.getById(sysUserVo.getUsrId());
            if (! sysUserVo1.getUsrPassword().equals(sysUserVo.getUsrPassword())){
                sysUserVo.setUsrPassword(MD5Unti.mD5Encryption(sysUserVo.getUsrPassword(), "czkt"));
            }
        } else if (sysUserVo.getUsrId()>=0) {
            sysUserVo.setUsrPassword(MD5Unti.mD5Encryption(sysUserVo.getUsrPassword(), "czkt"));
        }
        int userAdd = 0;
        int userUpdate = userService.getUserUpdate(sysUserVo);
        if (sysUserVo.getRole() != null) {
            userAdd = userService.getUserAdd(sysUserVo);
        }
        if (userUpdate > 0) {
            System.out.println("修改成功");
            return "redirect:/user/list";
        } else if (userAdd > 0) {
            System.out.println("添加成功");
            return "redirect:/user/list";
        } else if (userAdd <= 0) {
            return "redirect:/user/add";
        } else {
            return "redirect:/user/edit";
        }
    }

    @RequestMapping("/user/edit")
    public String getEdit(Model model, Long usrId) {
        model.addAttribute("roles", roleService.list());
        model.addAttribute("user", userService.UserSelectById(usrId));
        return "user/edit";
    }

    @GetMapping("/crm/user/del/{id}")
    @ResponseBody
    public Object deleteUser(@PathVariable Integer id) {
        int deleteUserCount = userService.getUserDelete(id);
        String delResult = "false";
        if (deleteUserCount > 0) {
            delResult = "true";
            System.out.println("删除成功！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("delResult", delResult);
        return map;
    }
}
